package com.jxl.application.controller.controller;


import com.jxl.application.controller.convert.SubjectAnswerConvert;
import com.jxl.application.controller.convert.SubjectInfoConvert;
import com.jxl.application.controller.dto.SubjectInfoDto;
import com.jxl.common.entity.PageResult;
import com.jxl.common.entity.Result;
import com.jxl.domain.entity.SubjectAnswerBO;
import com.jxl.domain.entity.SubjectInfoBO;
import com.jxl.domain.service.SubjectInfoDomainService;
import com.jxl.infra.basic.es.entity.SubjectInfoEs;
import com.jxl.infra.basic.es.util.EsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RequestMapping("/subject")
@RestController
@Slf4j
public class SubjectController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;





    /**
     * 题目添加
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDto subjectInfoDto){
        try{
            //进行类型转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoConvert.INSTANCE.convertDtoToSubjectInfoBO(subjectInfoDto);
            List<SubjectAnswerBO> subjectAnswerBOS =
                    SubjectAnswerConvert.INSTANCE.convertDtoToSubjectInfoBOList(subjectInfoDto.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOS);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok("插入成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 题目列表
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoBO>> querySubjectPage(@RequestBody SubjectInfoDto subjectInfoDto){
        try{
            //进行类型转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoConvert.INSTANCE.convertDtoToSubjectInfoBO(subjectInfoDto);
            PageResult<SubjectInfoBO> subjectInfoBOPageResult =
                    subjectInfoDomainService.querySubjectPage(subjectInfoBO);
            return Result.ok(subjectInfoBOPageResult);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 题目详情
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(@RequestBody SubjectInfoDto subjectInfoDto){
        try{
            //进行类型转换
            SubjectInfoBO subjectInfoBO =
                    SubjectInfoConvert.INSTANCE.convertDtoToSubjectInfoBO(subjectInfoDto);
            SubjectInfoBO subjectInfoBO1 = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDto subjectInfoDto1 = SubjectInfoConvert.INSTANCE.convertBOToSubjectInfoDto(subjectInfoBO1);
            return Result.ok(subjectInfoDto1);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 全文检索
     * @param subjectInfoDto
     * @return
     */
    @PostMapping("/getSubjectPageBySearch")
    public Result<PageResult<SubjectInfoEs>> getSubjectPageBySearch(@RequestBody SubjectInfoDto subjectInfoDto){
        //进行类型转换
        SubjectInfoBO subjectInfoBO =
                SubjectInfoConvert.INSTANCE.convertDtoToSubjectInfoBO(subjectInfoDto);
        PageResult<SubjectInfoEs> subjectInfoEsPageResult = subjectInfoDomainService.querySubejctInfoEs(subjectInfoBO);
        return Result.ok(subjectInfoEsPageResult);
    }

    /**
     * 获取题目贡献榜
     */
    @PostMapping("/getContributeList")
    public Result<List<SubjectInfoDto>> getContributeList() {
        try {
            List<SubjectInfoBO> boList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDto> dtoList = SubjectInfoConvert.INSTANCE.convertBOToDTOList(boList);
            return Result.ok(dtoList);
        } catch (Exception e) {
            log.error("SubjectCategoryController.getContributeList.error:{}", e.getMessage(), e);
            return Result.fail("获取贡献榜失败");
        }
    }

}
